package ve.com.wayoyo.wuml_case;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompClase;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompMetodo;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompVariable;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.Diagrama;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.FactoryDiagrama;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.VistaDiagrama;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;

public class NuevoDiagrama extends Activity {

	protected static final String MY_PACKAGE_NAME = "paquete";
	protected static final String MY_FILE_NAME = "file.txt";
	private String tipo;
	private VistaDiagrama DiagramaActual;
	private Button bt;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		this.tipo = intent.getStringExtra(Menu_Principal.TIPO_DIAGRAMA);
		setContentView(R.layout.activity_nuevo_diagrama);
		Display display = getWindowManager().getDefaultDisplay();
		UserPref.getInstancia().setWinHeight(display.getHeight());
		UserPref.getInstancia().setWinWidth(display.getWidth());
		UserPref.getInstancia().setUsingConvections(true);
		Diagrama diag = FactoryDiagrama.factory(tipo);
		DiagramaActual = new VistaDiagrama(this, diag);
		LinearLayout canvas = (LinearLayout) findViewById(R.id.Canvaslayout);
		this.menuPrincipal();
		canvas.addView(DiagramaActual);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_nuevo_diagrama, menu);
		return true;
	}

	public void menuPrincipal() {
		LinearLayout fondo = (LinearLayout) findViewById(R.id.buttonlayout);
		fondo.setBackgroundColor(Color.BLUE);
		LinearLayout layout = (LinearLayout) findViewById(R.id.buttonlayout2);
		layout.removeAllViewsInLayout();
		this.nuevaClase(layout);
//		this.exportar(layout);
		
	}

	public void cambioMenu() {
		LinearLayout fondo = (LinearLayout) findViewById(R.id.buttonlayout);
		fondo.setBackgroundColor(Color.GREEN);
		LinearLayout layout = (LinearLayout) findViewById(R.id.buttonlayout2);
		layout.removeAllViewsInLayout();
		layout.setBackgroundColor(Color.GRAY);
		this.agregarAtributo(layout);
		this.modificarAtributo(layout);
		this.agregarMetodo(layout);
		this.modificarMetodo(layout);
		this.conectar(layout);	
	}	

	public void nuevaClase(LinearLayout layout) {
		bt = new Button(this);
		bt.setText("Clase");

		bt.setWidth(130);
		bt.setHeight(40);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				DiagramaActual.nuevaClase();
			}
		});
		layout.addView(bt);
	}
	
	public void exportar(LinearLayout layout) {
		bt = new Button(this);
		bt.setText("Exportar");
		bt.setWidth(130);
		bt.setHeight(40);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String str = DiagramaActual.exportar();
				try{
					File file = new File(Environment.getExternalStorageDirectory()+"/WUML","faith.txt");
				if(!file.exists())
					file.createNewFile();
				
				FileWriter out = new FileWriter(file);
				out.write(str);
				out.close();
				}catch(Exception e)
				{
					
				}

			}
		});
		layout.addView(bt);
	}

	public void agregarAtributo(LinearLayout layout) {
		bt = new Button(this);
		bt.setWidth(130);
		// bt.setHeight(40);
		bt.setText("Agregar Atributo");
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				DiagramaActual.agregarAtributo();
			}
		});
		layout.addView(bt);
	}

	public void modificarAtributo(LinearLayout layout) {
		bt = new Button(this);
		bt.setText("Modificar Atributo");

		bt.setWidth(130);
		bt.setHeight(60);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				DiagramaActual.modificarAtributo();
			}
		});
		layout.addView(bt);
	}

	public void conectar(LinearLayout layout) {
		bt = new Button(this);
		bt.setText("Conectar");

		bt.setWidth(130);
		bt.setHeight(40);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				DiagramaActual.Conexion();
			}
		});
		layout.addView(bt);
	}
	
	public void agregarMetodo(LinearLayout layout) {
		bt = new Button(this);
		bt.setWidth(130);
		// bt.setHeight(40);
		bt.setText("Agregar Metodo");
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				DiagramaActual.agregarMetodo();
			}
		});
		layout.addView(bt);
	}

	private void modificarMetodo(LinearLayout layout) {
		bt = new Button(this);
		bt.setWidth(130);
		// bt.setHeight(40);
		bt.setText("Modificar Metodo");
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {				
				String [] values = NuevoDiagrama.this.getMethodList();
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
				android.R.layout.simple_list_item_1, android.R.id.text1, values);
				DiagramaActual.modificarMetodo(adapter);
			}
		});
		layout.addView(bt);		
	}
	
	public String[] getMethodList() {
		ArrayList<String> listString = new ArrayList<String>();
		ArrayList<CompMetodo> listMethod = ((CompClase)VistaDiagrama.getVista().getDiagrama().getSelectedItem()).get_metodos();
		String metodo;
		for (CompMetodo compMetodo : listMethod) {
			metodo = compMetodo.getNombreMetodo()+" (";
			for (CompVariable variable : compMetodo.getVariablesEntrada()) {
				if (!variable.equals(compMetodo.getVariablesEntrada().get(compMetodo.getVariablesEntrada().size()-1)))
					metodo=metodo+variable.getTipoDato()+",";
				else
					metodo=metodo+variable.getTipoDato()+")";
			}
			listString.add(metodo);
		}		
		String[] result = new String[]{};
		result=listString.toArray(result); 
		return result;
	}
	
}
